package com.ja.openchat.ui.chat;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.blankj.utilcode.util.BarUtils;
import com.ja.openchat.R;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebViewClient;
import com.trello.rxlifecycle4.components.support.RxFragmentActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PreviewWebViewActivity extends RxFragmentActivity {
    private String url;

//    @BindView(R.id.webview)
//    WebView webView;
    @BindView(R.id.webLayout)
    FrameLayout webLayout;

    public static void loadUrl(Context context, String title, String url) {
        Intent intent = new Intent(context, PreviewWebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    private AgentWeb mAgentWeb;

    public void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            BarUtils.setStatusBarColor(this, Color.WHITE);
        } else {
            BarUtils.setStatusBarColor(this, Color.BLACK);
        }
        BarUtils.setStatusBarLightMode(this, true);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar();
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        findViewById(R.id.iv_bar_back).setOnClickListener(v -> finish());
        Toolbar toolbar = findViewById(R.id.toolbar);
        url = getIntent().getStringExtra("url");
//        String htmlContent = getIntent().getStringExtra("content");
//        if (!TextUtils.isEmpty(htmlContent)) {
//            webView.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", "");
//        } else {
//            webView.loadUrl(url);
//        }

        String title = getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            toolbar.setTitle(title);
            setTitle(title);
        }
//        WebSettings settings = webView.getSettings();
//        settings.setJavaScriptEnabled(true);
//        WebViewClient webViewClient = new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url == null) return false;
//
//                try{
//                    if(!url.startsWith("http://") && !url.startsWith("https://")){
//                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                        startActivity(intent);
//                        return true;
//                    }
//                }catch (Exception e){//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
//                    return true;//没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
//                }
//
//                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                view.loadUrl(url);
//                return true;
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                String webTitle = view.getTitle();
//                if (!TextUtils.isEmpty(webTitle)) {
//                    if (!TextUtils.equals(webTitle, "about:blank")) {
//                        setTitle(webTitle);
//                    }
//                }
//            }
//        };
//        webView.setWebViewClient(webViewClient);

        if(!TextUtils.isEmpty(url)){
            if(url.startsWith("www")){
                url = "http://"+url;
            }
        }

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(webLayout, new FrameLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .setWebViewClient(new WebViewClient(){
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        handler.proceed();
                    }
                })
                .createAgentWeb()
                .ready()
                .go(url);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }
    public void setTitle(String title) {
        TextView titleText = findViewById(R.id.tv_bar_title);
        if (titleText != null) {
            titleText.setText(title);
        }
    }

}
